home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3393 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: using MALLOC() w/ 2-d arrays
  5. Date: 28 Jan 1996 17:49:13 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4egcup$d6r@news.iag.net>
  8. References: <4e9nm2$nna@cville-srv.wam.umd.edu>
  9. NNTP-Posting-Host: pm2-orl19.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4e9nm2$nna@cville-srv.wam.umd.edu>, jsquires@wam.umd.edu says...
  13. ->
  14. ->I have a function that takes as one of its arguments:
  15. ->
  16. ->unsigned char image[MAX_Y][MAX_X}
  17. ->
  18. ->
  19. ->I have a number of images which is determined at run time,
  20. ->therefore I can't declare the size of image_array[][MAX_Y][MAX_X].
  21. ->
  22. ->In other words, I want to be able to allocate space for as many
  23. ->of these images as I need at run time.  I've tried everything
  24. ->(except the correct way).
  25. ->
  26. ->I think I need something of the form:
  27. ->
  28. ->unsigned char * image_array[MAX_Y][MAX_X];
  29. ->
  30. ->image_array = (unsigned char *) malloc(.....sizeof(unsigned char));
  31. ->
  32. ->
  33. ->I know that if I declare:
  34. ->unsigned char image_array[10][MAX_Y][MAX_X];
  35. ->
  36. ->I can successfully send this to the function, but what
  37. ->if I don't know that I'll need ten?  Can anyone tell
  38. ->me the proper solution?  Thank you.
  39. ->
  40. ->
  41.  
  42. The c.l.c faq (Frequently Asked Question) list has some explanations on the
  43. allocation and passing of multidimensional arrays.  You might want to read
  44. through it.  It is available for anonymous ftp from rtfm.mit.edu
  45. /pub/usenet/comp.lang.c.
  46.  
  47. Probably the most readable solution would be to create a typedef for 
  48. image.  Maybe:
  49.  
  50.    typedef unsigned char image[MAX_Y][MAX_X];
  51.  
  52. Then you would use image like any other type;
  53.  
  54.    image imageObj, imageAry[8], *imagePtr; 
  55.  
  56.    imageObj[1][3] = 4;  
  57.    imagePtr = malloc( 5 * sizeof(image)); /* array of 5 images */
  58.  
  59. int ImageFunc( int imageCnt, image *ary); /* declare */
  60. ImageFunc( 5, imagePtr);                  /* call */
  61. ImageFunc( 8, imageAry);
  62.  
  63. -- 
  64. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  65. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  66.  
  67.